package com.firewolf.mybatis;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import com.firewolf.mybatis.mapper.UserMapper;

/**
 * @Description: 测试类
 * @Author: 刘兴
 * @Date : 2016年8月17日
 * @package : com.firewolf.mybatis.curd.drivers
 * @Java_Version : 1.7
 * @Version : 2016年8月17日下午12:53:58
 */
public class UserDriver {
	
	private SqlSession session = null;
	
	@Before
	public void init() throws IOException{
		session = MyBatisUtil.getSession("xmlConf.xml");
	}
	
	/**
	 * 传递单个参数，使用简单类传递
	 */
	@Test
	public void testSimpleValue(){
		User u = session.selectOne("userMapper.findById", 1);
		System.out.println(u);
	}
	
	@Test
	public void testJavaBean(){
		User conditionUser = new User();
		conditionUser.setAge(12);
		conditionUser.setName("Tom");
		User u = session.selectOne("userMapper.findByNameAndAge", conditionUser);
		System.out.println(u);
	}
	
	@Test
	public void testHashMap(){
		Map<String,Object> conditionMap = new HashMap<>();
		conditionMap.put("age", 12);
		conditionMap.put("name", "Tom");
		User u = session.selectOne("userMapper.findByNameAndAge2", conditionMap);
		System.out.println(u);
	}
	
	
	/**
	 * 使用接口查询的方式
	 */
	@Test
	public void testInterface(){
		UserMapper mapper = session.getMapper(UserMapper.class);
		User u = mapper.findByNameAndAge(12, "Tom");
		System.out.println(u);
	}
}
